<?php
namespace app\index\service;


use app\index\model\AccountListModel;
use app\index\model\EnginBondFanModel;
use app\index\model\EnginBondJiaoModel;
use app\index\model\LabourJieModel;
use app\index\model\PaymentBaoModel;
use app\index\model\PaymentDayModel;
use app\index\model\PaymentEnginModel;
use app\index\model\PaymentGongModel;
use app\index\model\PaymentJieModel;
use app\index\model\PaymentNeifuModel;
use app\index\model\PaymentNeikouModel;
use app\index\model\SubbaoJieModel;

class PaymentService extends CommonService
{

    public function get_paydata_paytype(){
        return [
            3 => '项目付款',
            5 => '日常付款',
            6 => '内包付款',
        ];
    }

    public function get_paydata_paytype_name($paytype){
        $arr = $this->get_paydata_paytype();
        $str = isset($arr[$paytype]) ? $arr[$paytype] : '';
        return $str;
    }

    public function get_paybao_paytype(){
        return [
            4 => '项目费用报销',
            15 => '公司费用报销',
        ];
    }

    public function get_paybao_paytype_name($paytype){
        $arr = $this->get_paybao_paytype();
        $str = isset($arr[$paytype]) ? $arr[$paytype] : '';
        return $str;
    }

    public function get_paydata_paystatus_name($money, $payfee){
        $str = '';
        if($payfee == 0){
            $str = '未支付';
        }else{
            if($payfee < $money){
                $str = '部分支付';
            }else{
                $str = '全部支付';
            }
        }
        return $str;
    }

    public function get_paydata_huanstatus_name($money, $payfee){
        $str = '';
        if($payfee == 0){
            $str = '未到账';
        }else{
            if($payfee < $money){
                $str = '部分到账';
            }else{
                $str = '已到账';
            }
        }
        return $str;
    }


    public function get_payment_budid(){
        return [
            1 => '材料费',
            2 => '人工费',
            3 => '机械费',
            4 => '分包费',
            5 => '措施费',
        ];
    }
    public function get_payment_budid_name($budid){
        $arr = $this->get_payment_budid();
        $str = isset($arr[$budid]) ? $arr[$budid] : '';
        return $str;
    }



    public function create_payment_engin_number(){
        $tou = 'XMFK';
        $daystr = substr(date("Ymd"), 2);
        $number = PaymentEnginModel::where('number','like','%'.$tou.$daystr.'%')->order('number DESC')->value('number');
        if(!$number){
            $str = $tou.$daystr.'00001';
        }else{
            $str = $tou.((int)str_replace($tou,'',$number)+1);
        }
        return $str;
    }

    public function create_payment_bao_number(){
        $tou = 'XMFYBX';
        $daystr = substr(date("Ymd"), 2);
        $number = PaymentBaoModel::where('number','like','%'.$tou.$daystr.'%')->order('number DESC')->value('number');
        if(!$number){
            $str = $tou.$daystr.'00001';
        }else{
            $str = $tou.((int)str_replace($tou,'',$number)+1);
        }
        return $str;
    }

    public function create_payment_gong_number(){
        $tou = 'GSFYBX';
        $daystr = substr(date("Ymd"), 2);
        $number = PaymentGongModel::where('number','like','%'.$tou.$daystr.'%')->order('number DESC')->value('number');
        if(!$number){
            $str = $tou.$daystr.'00001';
        }else{
            $str = $tou.((int)str_replace($tou,'',$number)+1);
        }
        return $str;
    }

    public function create_payment_day_number(){
        $tou = 'RCFK';
        $daystr = substr(date("Ymd"), 2);
        $number = PaymentDayModel::where('number','like','%'.$tou.$daystr.'%')->order('number DESC')->value('number');
        if(!$number){
            $str = $tou.$daystr.'00001';
        }else{
            $str = $tou.((int)str_replace($tou,'',$number)+1);
        }
        return $str;
    }

    public function create_payment_neikou_number(){
        $tou = 'NBKK';
        $daystr = substr(date("Ymd"), 2);
        $number = PaymentNeikouModel::where('number','like','%'.$tou.$daystr.'%')->order('number DESC')->value('number');
        if(!$number){
            $str = $tou.$daystr.'00001';
        }else{
            $str = $tou.((int)str_replace($tou,'',$number)+1);
        }
        return $str;
    }

    public function create_payment_neifu_number(){
        $tou = 'NBFK';
        $daystr = substr(date("Ymd"), 2);
        $number = PaymentNeifuModel::where('number','like','%'.$tou.$daystr.'%')->order('number DESC')->value('number');
        if(!$number){
            $str = $tou.$daystr.'00001';
        }else{
            $str = $tou.((int)str_replace($tou,'',$number)+1);
        }
        return $str;
    }


    public function create_bond_jiao_number(){
        $tou = 'XMBZJJN';
        $daystr = substr(date("Ymd"), 2);
        $number = EnginBondJiaoModel::where('number','like','%'.$tou.$daystr.'%')->order('number DESC')->value('number');
        if(!$number){
            $str = $tou.$daystr.'00001';
        }else{
            $str = $tou.((int)str_replace($tou,'',$number)+1);
        }
        return $str;
    }
    public function get_engin_bondtype(){
        return [
            1 => '投标保证金',
            2 => '履约保证金',
            3 => '农民工工资保证金',
        ];
    }
    public function get_engin_bondtype_name($bondtype){
        $arr = $this->get_engin_bondtype();
        $str = isset($arr[$bondtype]) ? $arr[$bondtype] : '';
        return $str;
    }




    public function create_bond_fan_number(){
        $tou = 'XMBZJFH';
        $daystr = substr(date("Ymd"), 2);
        $number = EnginBondFanModel::where('number','like','%'.$tou.$daystr.'%')->order('number DESC')->value('number');
        if(!$number){
            $str = $tou.$daystr.'00001';
        }else{
            $str = $tou.((int)str_replace($tou,'',$number)+1);
        }
        return $str;
    }


    public function create_payment_jie_number(){
        $tou = 'BYJ';
        $daystr = substr(date("Ymd"), 2);
        $number = PaymentJieModel::where('number','like','%'.$tou.$daystr.'%')->order('number DESC')->value('number');
        if(!$number){
            $str = $tou.$daystr.'00001';
        }else{
            $str = $tou.((int)str_replace($tou,'',$number)+1);
        }
        return $str;
    }


    public function create_account_list_jiaonum_zhi(){
        $tou = 'ZC';
        $daystr = substr(date("Ymd"), 2);
        $number = AccountListModel::where('jiaonum','like','%'.$tou.$daystr.'%')->order('jiaonum DESC')->value('jiaonum');
        if(!$number){
            $str = $tou.$daystr.'00001';
        }else{
            $str = $tou.((int)str_replace($tou,'',$number)+1);
        }
        return $str;
    }



    public function get_yiju_qingfee($yijuid=0, $yiju='', $paymentid=0){
        if($yiju == 'labourjie'){
            $caiid = LabourJieModel::where(['id'=>$yijuid])->value('caiid');
            $qingfee = PaymentEnginModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1])
                ->where('status','in',[0,1])
                ->where('id','neq',$paymentid)
                ->where('yiju','in',['concailabour','labourshen','labourjie','labourling','labouryong'])
                ->where(['caiid'=>$caiid])
                ->sum('money');
        }elseif($yiju == 'subbaojie'){
            $caiid = SubbaoJieModel::where(['id'=>$yijuid])->value('caiid');
            $qingfee = PaymentEnginModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1])
                ->where('status','in',[0,1])
                ->where('id','neq',$paymentid)
                ->where('yiju','in',['concaisubbao','subbaoshen','subbaojie'])
                ->where(['caiid'=>$caiid])
                ->sum('money');
        }else{
            $qingfee = PaymentEnginModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1])
                ->where('status','in',[0,1])
                ->where('id','neq',$paymentid)
                ->where(['yiju'=>$yiju, 'yijuid'=>$yijuid])
                ->sum('money');
        }



        return $qingfee;
    }

}

